package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Course;
import com.qk.management.service.impl.CourseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/courses")
@SuppressWarnings("all")
public class CourseController {
    @Autowired
    private CourseServiceImpl courseService;

    @PostMapping
    public Result addCourse(@RequestBody Course course){
        log.info("添加课程信息:{}", course);
        courseService.addCourse(course);
        return Result.success();
    }

    @GetMapping
    public Result page(

            @RequestParam(name = "name", required = false) String name,
            @RequestParam(name = "subject", required = false) Integer subject,
            @RequestParam(name = "target", required = false) Integer target,
            @RequestParam(name = "page", required = true, defaultValue = "1") Integer page,
            @RequestParam(name = "pageSize", required = true, defaultValue = "10") Integer pageSize){
        PageResult<Course> PageResult= courseService.page(name, subject, target, page, pageSize);
        log.info("分页查询课程,参数:课程名称={},课程学科={},适用人群={},当前页码={},每页大小={}",name,subject, target, page, pageSize);
        return Result.success(PageResult);

    }
    @GetMapping("/{id}")
    public Result selectId(@PathVariable("id") Integer id){
        log.info("查询课程id为：{}",id);
        Course course =courseService.selectId(id);
        return Result.success(course);
    }

    @PutMapping
    public Result updateCourse(@RequestBody Course course){
        log.info("修改课程信息为：{}",course);
        courseService.updateCourse(course);
        return Result.success();
    }
    @DeleteMapping("/{id}")
    public Result deleteCourse(@PathVariable Integer id){
        log.info("删除课程id为：{}",id);
        courseService.deleteCourse(id);
        return Result.success();
    }
    @GetMapping("/list")
    public Result list(){
        log.info("查询所有课程");
        return Result.success(courseService.list());
    }

    @GetMapping("/subject/{subject}")
    public Result selectSubject(@PathVariable Integer subject){
        log.info("查询课程学科为：{}",subject);
        return Result.success(courseService.selectSubject(subject));
    }
}
